package com.xiaotao.user.controller;

import com.xiaotao.common.Result;
import com.xiaotao.user.domain.dto.PageDTO;
import com.xiaotao.user.domain.dto.UserDTO;
import com.xiaotao.user.domain.po.User;
import com.xiaotao.user.domain.query.PageQuery;
import com.xiaotao.user.domain.vo.UserListVO;
import com.xiaotao.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

//    @Resource
//    UserService userService;

    final UserService userService;


    // Result 是统一返回结果类，在common-module/src/main/java/com/xiaotao/common/Result.java
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO dto){
        User dbUser = null;
        dbUser = userService.login(dto);
        return Result.success(dbUser);
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserDTO dto){
        userService.register(dto);
        return Result.success();
    }


    @GetMapping("/data")
    public String getData() {
        return "Hello from Spring Boot!";
    }



    // 新增商品
    @PostMapping("/add")
    public void addUser(@RequestBody UserDTO dto){
        userService.addUser(dto);
    }

    // 删除商品
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
    }

    // 查询是否有这条商品 根据商品id查询，有返回1，无返回0
    @GetMapping("/{id}")
    public String selectUserById(@PathVariable Long id){
        return userService.selectUserById(id) + "";
    }


    //测试接口
    @GetMapping("/test")
    public String test() {
        return "Backend is running!";
    }

    // 获取全部的商品列表
    @GetMapping
    public List<UserListVO> getUsers() {
        return userService.getUsers();
    }

    // 分页获取商品列表
    @PostMapping("/page")
    public PageDTO<UserListVO> getUsersByPageDTO(@Validated @RequestBody PageQuery query){
        return userService.getUsersByPageDTO(query);
    }
}
